package com.bq.gametrade.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.CustomerServiceDialog;
import com.bq.gametrade.utils.SharedPreferencesUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";


    private IWXAPI api;

    private int count;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_results);
        api = WXAPIFactory.createWXAPI(this, "wx69de18f9a4f26554");//appid需换成商户自己开放平台appid
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            // resp.errCode == -1 原因：支付错误,可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
            // resp.errCode == -2 原因 用户取消,无需处理。发生场景：用户不支付了，点击取消，返回APP
            if (resp.errCode == 0) // 支付成功
            {
                queryOrder();
                finish();
            } else {
                toastShow("取消了支付");

                finish();
            }
        }
    }

    private void queryOrder() {

        String orderId = "";
        if (!SharedPreferencesUtils.getValue(WXPayEntryActivity.this, SharedPreferencesUtils.pay_order, "").equals("")) {
            orderId = (String) SharedPreferencesUtils.getValue(WXPayEntryActivity.this, SharedPreferencesUtils.pay_order, "");
        }
        Map<String, String> params = new HashMap<>();
        params.put("token", application.getUserInfo().token);
        params.put("orderId", orderId);

        showSpotsDialog("正在加载····");
        VRHttp.sendRequest(WXPayEntryActivity.this, HttpLink.PAYORDER_QUERY, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    closeSpotsDialog();
                    ResponseBean responseBean = (ResponseBean) response;
                    if (responseBean.status.equals("0")) {
                        toastShow("恭喜你！充值成功");
                        finish();
                    }else {
                        count++;
                        if(count<3){
                        queryOrder();
                        }else {
                            toastShow("充值失败请联系客服");
                            CustomerServiceDialog dialog = new CustomerServiceDialog();
                            dialog.show(getSupportFragmentManager(),"");
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferencesUtils.saveValue(WXPayEntryActivity.this,SharedPreferencesUtils.pay_order,"");
    }
}